package com.subject.shuati.domain.handler.subject;

import com.subject.shuati.common.enums.IsDeleteFlagEnum;
import com.subject.shuati.common.enums.SubjectInfoTypeEnum;
import com.subject.shuati.domain.covert.MultipleSubjectConverter;
import com.subject.shuati.domain.entity.SubjectAnswerBo;
import com.subject.shuati.domain.entity.SubjectInfoBo;
import com.subject.shuati.domain.entity.SubjectOptionBo;
import com.subject.shuati.infra.basic.entity.SubjectMultiple;
import com.subject.shuati.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 多选题目的策略模式实现类
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultipleService multipleService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        try {
            List<SubjectMultiple> subjectMultiples = new ArrayList<>();
            subjectInfoBo.getOptionList().forEach(option->{
                SubjectMultiple subjectMultiple = MultipleSubjectConverter.INSTANCE.BoConverterToEntity(option);
                subjectMultiple.setSubjectId(subjectInfoBo.getId());
                subjectMultiple.setIsDeleted(IsDeleteFlagEnum.UN_DELETE.getCode());
                subjectMultiples.add(subjectMultiple);
            });
            multipleService.batchInsert(subjectMultiples);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public SubjectOptionBo query(int subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId((long) subjectId);
        List<SubjectMultiple> result = multipleService.queryByCondition(subjectMultiple);
        List<SubjectAnswerBo> subjectAnswerBOList = MultipleSubjectConverter.INSTANCE.EntityConverterToBoList(result);
        SubjectOptionBo subjectOptionBO = new SubjectOptionBo();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;

    }
}
